@echo off
chcp 65001 >nul
title 社区服务平台开机自启动

echo ========================================
echo 社区服务平台开机自启动
echo ========================================
echo.

echo 🔧 检查MySQL数据库...
netstat -an | findstr :3306 >nul 2>&1
if errorlevel 1 (
    echo MySQL未运行，正在启动...
    cd /d "D:\phpstudy_pro\Extensions\MySQL8.0.12\bin"
    start /B mysqld.exe --console
    echo 等待MySQL启动...
    timeout /t 15 /nobreak >nul
) else (
    echo MySQL已在运行
)

echo.
echo 🔧 启动Node.js服务器...
cd /d "C:\Users\14854\Desktop\h6666\backend"

echo 检查PM2进程...
pm2 list | findstr "neighborhood-help-backend" >nul 2>&1
if errorlevel 1 (
    echo 启动服务器...
    pm2 start server.js --name "neighborhood-help-backend"
) else (
    echo 服务器已在运行，重启服务...
    pm2 restart neighborhood-help-backend
)

echo.
echo 等待服务器启动...
timeout /t 5 /nobreak >nul

echo.
echo ========================================
echo 开机自启动完成！
echo ========================================
echo.
echo 📡 服务器地址: http://localhost:3002/api
echo 🌐 网络地址: http://192.168.1.3:3002/api
echo.
echo 管理命令:
echo   pm2 status    - 查看状态
echo   pm2 logs      - 查看日志
echo   pm2 restart   - 重启服务
echo   pm2 stop      - 停止服务
echo ========================================
